package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.dto.StudentDTO;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.entity.StudentExample;
import com.hzit.mapper.ClassesMapper;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.IStudentService;
import com.hzit.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements IStudentService {

	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private ClassesMapper classesMapper;

	//1. 列表所有学生
	@Override
	public List<Student> findAll() {
		return studentMapper.selectByExample(null);
	}

	//2. 分页查询
	@Override
	public PageResult<Student> findByPage(int page, int pageSize) {
		//2.1 开始分页
		PageHelper.startPage(page,pageSize);
		//2.2 查询所有记录
		List<Student> students = studentMapper.selectByExample(null);
		//2.3 转换为Page对象
		Page<Student> studentPage = (Page<Student>) students;
		//2.4 返回结果
		return new PageResult<>(studentPage.getResult(),studentPage.getTotal());
	}

	//3. 条件查询带分页
	@Override
	public PageResult<Student> search(int page, int pageSize, StudentVo vo) {
		//3.1 开始分页
		PageHelper.startPage(page,pageSize);
		//3.2 按照条件查询所有记录
		//3.2.1 构造查询条件对象
		StudentExample example = new StudentExample();
		//3.2.2 如果本次查询带有条件就构造一个StudentExample.Criteria对象来封装本次查询条件
		StudentExample.Criteria criteria = example.createCriteria();
		//3.2.3 为查询条件赋值
		if(vo != null){
			if(StrUtil.isNotBlank(vo.getSname())){
				criteria.andSnameLike("%" + vo.getSname() + "%");
			}
			if(StrUtil.isNotBlank(vo.getAddr())){
				criteria.andAddrLike("%" + vo.getAddr() + "%");
			}
			if(vo.getCid() != null && vo.getCid() != 0){
				criteria.andCidEqualTo(vo.getCid());
			}
		}
		//3.3 开始进行条件查询
		List<Student> students = studentMapper.selectByExample(example);
		//3.3 转换为Page对象
		Page<Student> studentPage = (Page<Student>) students;
		//3.4 返回结果
		return new PageResult<>(studentPage.getResult(),studentPage.getTotal());
	}

	//3. 标准开发版本（使用到了领域模型）: 处理了班级名称
	@Override
	public PageResult<StudentDTO> search2(int page, int pageSize, StudentVo vo) {
		//3.1 开始分页
		PageHelper.startPage(page,pageSize);
		//3.2 按照条件查询所有记录
		//3.2.1 构造查询条件对象
		StudentExample example = new StudentExample();
		//3.2.2 如果本次查询带有条件就构造一个StudentExample.Criteria对象来封装本次查询条件
		StudentExample.Criteria criteria = example.createCriteria();
		//3.2.3 为查询条件赋值
		if(vo != null){
			if(StrUtil.isNotBlank(vo.getSname())){
				criteria.andSnameLike("%" + vo.getSname() + "%");
			}
			if(StrUtil.isNotBlank(vo.getAddr())){
				criteria.andAddrLike("%" + vo.getAddr() + "%");
			}
			if(vo.getCid() != null && vo.getCid() != 0){
				criteria.andCidEqualTo(vo.getCid());
			}
		}
		//3.3 开始进行条件查询
		List<Student> students = studentMapper.selectByExample(example);
		//3.4 转换为Page对象
		Page<Student> studentPage = (Page<Student>) students;
		//3.5 将分页中的结果由List<Student>转换为List<StudentDTO>类型
		List<Student> result = studentPage.getResult();
		//将List<Student>转换为List<StudentDTO>
		//转换方法一：
		List<StudentDTO> studentDTOS = new ArrayList<>();
		for (Student student : result) {
			StudentDTO dto = new StudentDTO();
			//3.5.1 将student对象中的所有属性对考到对应的dto对象同名属性中
			BeanUtil.copyProperties(student,dto,true);
			//3.5.2 为dto设置班级名称字段
			Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
			dto.setCname(classes.getCname());
			//3.5.3 将dto添加到集合中
			studentDTOS.add(dto);
		}
		//转换方法二：
		List<StudentDTO> collect = result.stream().map(m -> {
			StudentDTO dto = new StudentDTO();
			//3.5.1 将student对象中的所有属性对考到对应的dto对象同名属性中
			BeanUtil.copyProperties(m, dto, true);
			//3.5.2 为dto设置班级名称字段
			Classes classes = classesMapper.selectByPrimaryKey(m.getCid());
			dto.setCname(classes.getCname());
			return dto;
		}).collect(Collectors.toList());
		//3.6 返回结果
		return new PageResult<>(studentDTOS,studentPage.getTotal());
	}
}
